﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;

namespace MWOServerStatus
{
    public partial class MWOServerStatus : Form
    {
        private enum ServerStatus
        {
            Unknown     = -2,
            Maintenance = -1,
            Offline     = 0,
            Online      = 1,
        }

        private ServerStatus serverStatus = ServerStatus.Unknown;
        private ServerStatus lastStatus   = ServerStatus.Unknown;

        public MWOServerStatus()
        {
            InitializeComponent();
        }

        private void MWOServerStatus_Load(object sender, EventArgs e)
        {
            UpdateForm();
        }
        
        private void UpdateForm()
        {
            serverStatus = FetchStatus();
            UpdateLabel();
            UpdateNotifier();
        }

        private void UpdateNotifier()
        {
            if (lastStatus != serverStatus)
            {
                Notifier.BalloonTipTitle = "Mechwarrior Online";
                Notifier.BalloonTipIcon = ToolTipIcon.Info;
                
                switch (serverStatus)
                {
                    case ServerStatus.Unknown:
                        Notifier.BalloonTipText = "Server Status: unknown";
                        FetchTimer.Interval = 30 * 1000;
                        break;
                    case ServerStatus.Maintenance:
                        Notifier.BalloonTipText = "Server Status: under maintenance";
                        FetchTimer.Interval = 30 * 1000;
                        break;
                    case ServerStatus.Online:
                        Notifier.BalloonTipText = "Server Status: online";
                        FetchTimer.Interval = 2 * 60 * 1000;
                        break;
                    case ServerStatus.Offline:
                        Notifier.BalloonTipText = "Server Status: offline";
                        FetchTimer.Interval = 30 * 1000;
                        break;
                }

                Notifier.ShowBalloonTip(2000);
                lastStatus = serverStatus;
            }
        }

        private void UpdateLabel()
        {
            switch (serverStatus)
            {
                case ServerStatus.Unknown:
                    lblServerStatus.Text = "unknown";
                    lblServerStatus.ForeColor = Color.Orange;
                    break;
                case ServerStatus.Maintenance:
                    lblServerStatus.Text = "under maintenance";
                    lblServerStatus.ForeColor = Color.Brown;
                    break;
                case ServerStatus.Online:
                    lblServerStatus.Text = "online";
                    lblServerStatus.ForeColor = Color.Green;
                    break;
                case ServerStatus.Offline:
                    lblServerStatus.Text = "offline";
                    lblServerStatus.ForeColor = Color.Red;
                    break;
            }
        }

        private ServerStatus FetchStatus()
        {
            WebRequest request = WebRequest.Create("http://mwomercs.com/do/ajax/serverStatus.json");
            request.Method = "GET";
            
            try
            {
                WebResponse  response           = request.GetResponse();
                Stream       responseStream     = response.GetResponseStream();
                StreamReader responseReader     = new StreamReader(responseStream);
                string       responseFromServer = responseReader.ReadToEnd();
                ServerStatus status             = ServerStatus.Unknown;

                switch (responseFromServer)
                {
                    case "{\"server_status\":\"\"}":
                        status = ServerStatus.Maintenance;
                        break;
                    case "{\"server_status\":0}":
                        status = ServerStatus.Offline;
                        break;
                    case "{\"server_status\":1}":
                        status = ServerStatus.Online;
                        break;
                }

                return status;
            }
            catch
            {
                return ServerStatus.Offline;
            }
        }

        private void FetchTimer_Tick(object sender, EventArgs e)
        {
            UpdateForm();
        }

        private void MWOServerStatus_Resize(object sender, EventArgs e)
        {
            if (WindowState == FormWindowState.Minimized)
            {
                this.Hide();
            }
        }

        private void Notifier_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            this.Show();
            this.WindowState = FormWindowState.Normal;
        }
    }
}
